package com.ybf.user.dal.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ybf.user.dal.entity.RoleDO;
import com.ybf.user.dal.entity.query.RoleQueryDO;
import com.ybf.user.dal.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Component
public class RoleManager extends ServiceImpl<RoleMapper, RoleDO> {

    @Autowired
    private RoleMapper roleMapper;

    public List<RoleDO> selectInIds(List<Long> roleIds) {
        if (CollectionUtils.isEmpty(roleIds)) {
            return Collections.emptyList();
        }
        return roleMapper.selectBatchIds(roleIds);
    }

    /**
     * 列表
     *
     * @author 向南
     * @date 17:31 2023/7/28
     * @param: orgId
     * @param: type
     **/
    public List<RoleDO> selectByOrgIdAndType(Long orgId, Integer type) {
        if (Objects.isNull(orgId) || Objects.isNull(type)) {
            return Collections.emptyList();
        }
        return roleMapper.selectList(new LambdaQueryWrapper<RoleDO>()
                .eq(RoleDO::getOrgId, orgId)
                .eq(RoleDO::getType, type));
    }

    /**
     * 查询 - 分页
     *
     * @author 向南
     * @date 13:40 2023/7/31
     * @param: queryDO
     **/
    public IPage<RoleDO> selectPage(RoleQueryDO queryDO) {
        if (Objects.isNull(queryDO.getOrgId())) {
            return queryDO.getPage();
        }
        LambdaQueryWrapper<RoleDO> queryWrapper = new LambdaQueryWrapper<RoleDO>()
                .eq(RoleDO::getOrgId, queryDO.getOrgId()).eq(RoleDO::getType, queryDO.getType());
        if (!StringUtils.isEmpty(queryDO.getName())) {
            queryWrapper.like(RoleDO::getName, queryDO.getName());
        }
        return roleMapper.selectPage(queryDO.getPage(), queryWrapper);
    }
}
